namespace WinFormsApp1_自动定时
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            string url;
            pictureBox1.Image = GetScreenCapture(out url);


            ListViewItem listViewItem = new ListViewItem();
            string timeritem = System.Text.RegularExpressions.Regex.Replace(comboBox1.Text, @"[^0-9]+", "");
            listViewItem.Text = (int.Parse(timeritem)*60).ToString();

            listViewItem.SubItems.Add($"{comboBox1.Text}");

            
            listViewItem.SubItems.Add(url);
            listView1.Items.Add(listViewItem);





        }
        private Bitmap GetScreenCapture(out string url)
        {
            Rectangle tScreenRect = new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Bitmap tSrcBmp = new Bitmap(tScreenRect.Width, tScreenRect.Height); // 用于屏幕原始图片保存
            Graphics gp = Graphics.FromImage(tSrcBmp);
            gp.CopyFromScreen(0, 0, 0, 0, tScreenRect.Size);
            gp.DrawImage(tSrcBmp, 0, 0, tScreenRect, GraphicsUnit.Pixel);

            Random ran = new Random();

            url = @$"temp\{ran.Next(1, 999)}.jpg";
            tSrcBmp.Save(url);
            
            return tSrcBmp;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;



            listView1.Columns.Add("倒计时/秒",80,HorizontalAlignment.Left);
            listView1.Columns.Add("定时间");
            listView1.Columns.Add("图片地址", 100, HorizontalAlignment.Left);
            listView1.FullRowSelect = true;//只能选一行

            timer1.Interval = 1000;

            timer1.Start();
        }
       

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.listView1.BeginUpdate();   //数据更新，UI暂时挂起，直到EndUpdate绘制控件，可以有效避免闪烁并大大提高加载速度
            foreach (ListViewItem item in listView1.Items)
            {
                

                item.SubItems[0].Text = (int.Parse(item.SubItems[0].Text) - 1).ToString();

                //System.Diagnostics.Debug.WriteLine(item.SubItems[2].Text);
        

            }

            this.listView1.EndUpdate();  //结束数据处理，UI界面一次性绘制。

        }
        /// <summary>
        /// 双击显示图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(listView1.SelectedItems[0].SubItems[2].Text);

            pictureBox1.ImageLocation = listView1.SelectedItems[0].SubItems[2].Text;

         
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            File.Delete(listView1.SelectedItems[0].SubItems[2].Text);
            listView1.SelectedItems[0].Remove();

        }
    }
}